import { BehaviorSubject } from 'rxjs'
import _g from './global'
import * as _ from 'underscore'
import { webSocket } from 'rxjs/webSocket'

const page = new BehaviorSubject()
const comm = new BehaviorSubject()
const setting = new BehaviorSubject()

/**
 * 获取企业的ID
 * @returns {string}
 */
function getCompanyId() {
  const user = JSON.parse(localStorage.getItem('userInfo'))
  if (user) {
    console.log('company id', user.company.id)
    return String(user.company.id)
  } else {
    return ''
  }
}

/**
 * 获取企业的头像
 * @returns {string}
 */
function getCompanyIcon() {
  const user = JSON.parse(localStorage.getItem('userInfo'))
  if (user && user.company) {
    const icon = user.company.headImage
    console.log('company icon', icon)
    if (icon) {
      let fileName = icon.substring(icon.lastIndexOf('/') + 1, icon.indexOf('.'))
      return require('@/assets/img/setting/' + fileName + '.png')
    } else {
      return ''
    }
  } else {
    return ''
  }
}


/**
 * 获取企业顾问的信息
 * @returns {Array|*}
 */
function getAdviser() {
  const user = JSON.parse(localStorage.getItem('userInfo'))
  console.log('adviser', user.platformUser)
  return user.platformUser
}

const onlineQ = true
console.log('connect ws')
const ws = webSocket({
  //47.111.102.219
  url: onlineQ ? 'wss://web.gcs360.com/chat' : 'ws://web.gcs360.com/chat',
  openObserver: {
    next(t) {
      register()
      console.log('websocket opened.', t)
    }
  },
  closeObserver: {
    next() {
      console.log('websocket closed.')
    }
  }
})


function register() {
  console.log('start register')
  if (getCompanyId()) {
    const o = {
      'mainCom': 'company',
      'mainUserId': getCompanyId(),
      'type': 'REGISTER'
    }
    console.log('websocket register', o)
    ws.next(o)
  }
}

function offline() {
  const o = {
    'mainCom': 'company',
    'mainUserId': getCompanyId(),
    'type': 'REMOVE'
  }
  console.log('websocket offline', o)
  ws.next(o)
}

function loadStatic(type, cb) {
  _g.apiPost('/company/selectStaticData.do', { state: type })
    .then(res => {
      if (!_.isNull(res) && res.status === 0) {
        cb(res.data)
      }
    })
}

function log(text, obj) {
  console.log(text)
  console.log(obj)
}

function personInfoQ() {
  const company = JSON.parse(localStorage.getItem('userInfo')).company
  console.log('pre post info', company, company.email)
  if (company.contactName && company.sex !== '' && company.phone && company.email) {
    return true
  } else {
    return false
  }
}

function companyInfoQ() {
  const c = JSON.parse(localStorage.getItem('userInfo')).company
  if (c.fullName && c.typeId && c.natureId && c.sizeId && c.province && c.city && c.detailAddress && c.businessLicense) {
    return true
  } else {
    return false
  }
}

function getEmail() {
  const c = JSON.parse(localStorage.getItem('userInfo')).company
  if (c) {
    return c.email
  } else {
    return null
  }
}

function getCheckState() {
  const c = JSON.parse(localStorage.getItem('userInfo')).company
  if (c) {
    return Number(c.checkState)
  } else {
    return 0
  }
}

export default {
  getCheckState,
  getEmail,
  comm,
  page,
  loadStatic,
  log,
  setting,
  ws,
  register,
  offline,
  getAdviser,
  getCompanyId,
  personInfoQ,
  companyInfoQ,
  getCompanyIcon
}